package GUI.TabPanels;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import GUI.CustomerScreen;
import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class CustomerBookingPanel extends JPanel{
	private CustomerScreen customer_screen;

	private JLabel num_people_label;
	private JComboBox num_people_box;

	private JPanel start_day_panel;
	private JLabel start_day_label;
	private JComboBox start_day_box;
	private JComboBox start_month_box;
	private JComboBox start_year_box;

	private String [] days = {"01","02","03","04","05","06","07","08","09","10",
			"11","12","13","14","15","16","17","18","19","20",
			"21","22","23","24","25","26","27","28","29","30","31"};
	private String [] months = {"1","2","3","4","5","6","7","8","9","10","11","12"};
	private String [] years = {"2012", "2013", "2014", "2015", "2016", "2017"};

	private JPanel end_day_panel;
	private JLabel end_day_label;
	private JComboBox end_day_box;
	private JComboBox end_month_box;
	private JComboBox end_year_box;

	private JLabel hotel_label;
	private JComboBox hotel_box;

	private JButton book_room;

	public CustomerBookingPanel(){
		this.setLayout(new GridLayout(0,2));

		customer_screen = CustomerScreen.getInstance();

		hotel_label = new JLabel("Hotel:");
		this.add(hotel_label);
		hotel_box = new JComboBox(Controller.GetHotels());
		this.add(hotel_box);

		this.add(new JLabel());
		this.add(new JLabel());

		num_people_label = new JLabel("# of people");
		String [] numbers = {"1", "2", "3", "4"};
		num_people_box = new JComboBox(numbers);
		this.add(num_people_label);
		this.add(num_people_box);
		this.add(new JLabel());
		this.add(new JLabel());

		start_day_label = new JLabel("Start Day:");
		this.add(start_day_label);

		start_day_panel = new JPanel();
		start_day_box = new JComboBox(days);
		start_day_panel.add(start_day_box);
		start_month_box = new JComboBox(months);
		start_day_panel.add(start_month_box);
		start_year_box = new JComboBox(years);
		start_day_panel.add(start_year_box);
		this.add(start_day_panel);

		this.add(new JLabel());
		this.add(new JLabel());

		end_day_label = new JLabel("End Day:");
		this.add(end_day_label);

		end_day_panel = new JPanel();
		end_day_box = new JComboBox(days);
		end_day_panel.add(end_day_box);
		end_month_box = new JComboBox(months);
		end_day_panel.add(end_month_box);
		end_year_box = new JComboBox(years);
		end_day_panel.add(end_year_box);
		this.add(end_day_panel);

		this.add(new JLabel());
		this.add(new JLabel());

		book_room = new JButton("Book Room");
		book_room.addActionListener(new ViewAvailableRoomsButton());
		this.add(book_room);
	}

	public String getHotelID(){
		String hotel_id = (String) hotel_box.getSelectedItem();
		hotel_id = hotel_id.substring(1, hotel_id.indexOf(']'));
		return hotel_id;
	}

	public String[] getBookingDates(){
		String start_day = (String) start_year_box.getSelectedItem()+ "-" +
				(String) start_month_box.getSelectedItem()+ "-" +
				(String) start_day_box.getSelectedItem();
		String end_day = (String) end_year_box.getSelectedItem()+ "-" +
				(String) end_month_box.getSelectedItem()+ "-" +
				(String) end_day_box.getSelectedItem();
		String[] info = {
				start_day,
				end_day,					
		};
		// input[0] -> customerID
		// input[1] -> roomID
		// input[2] -> start_date
		// input[3] -> end_date

		return info;
	}

	private class ViewAvailableRoomsButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (new GregorianCalendar(Integer.valueOf(String.valueOf(start_year_box.getSelectedItem())),
					Integer.valueOf(String.valueOf(start_month_box.getSelectedItem())),
					1 + Integer.valueOf(String.valueOf(start_day_box.getSelectedItem()))).after(
							new GregorianCalendar(Integer.valueOf(String.valueOf(end_year_box.getSelectedItem())),
									Integer.valueOf(String.valueOf(end_month_box.getSelectedItem())),
									Integer.valueOf(String.valueOf(end_day_box.getSelectedItem()))))){
				JOptionPane.showMessageDialog(customer_screen, "Invalid Day Selection");
			}else{
				customer_screen.UpdateAvailableRooms();
				customer_screen.ViewRooms();
			}
		}
	}
}
